package com.heimali.happyreader.utils;

import java.sql.Date;
import java.text.SimpleDateFormat;

import com.heimali.happyreader.HappyReaderApplication;
import com.heimali.happyreader.config.Constants;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class SystemUtils {
	private static PackageManager pm = null;
	private static String versionName = null;
	private static int versionCode = 0;
	private static float density = 0;
	private static int screenWidth = 0;
	private static int screenHeight = 0;
	
	/**
	 * 终端屏幕分辨率
	 */
	public int baseScreenWidth = -1, baseScreenHeight = -1;
	/**
	 * 密度
	 */
	public float baseDensity = -1;
	
	public static void Log(Object... logs) {
		if (!Constants.IS_DEBUG) {
			return ;
		}
		
		String out = "";
		for(Object log : logs) {
			if (log == null) {
				out += "null , ";
			}else {
				out += log.toString() + " , ";
			}
		}
		System.out.println(out.substring(0, out.length()-3));
	}
	
	public static PackageManager getPackageManager() {
		if (pm == null)
			pm = HappyReaderApplication.getApplication().getBaseContext().getPackageManager();
		return pm;
	}

	public static PackageInfo getPackageInfo(String packageName) {
		try {
			if (packageName == null)
				packageName = HappyReaderApplication.getContext().getPackageName();
			return getPackageManager().getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static int getVersionCode() {
		if (versionCode == 0) {
			versionCode = getPackageInfo(null).versionCode;
		}
		return versionCode;
	}
	

	public static String getVersionName() {
		if (versionName == null) {
			versionName = getPackageInfo(null).versionName;
		}
		return versionName;
	}

	public static String getModel() {
		return android.os.Build.MODEL;
	}
	
	public static boolean isMounted() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	
	public static String getSDCardPath() {
		return Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.PATH_SEPERATOR;
	}
	
	private static void getScreenInfo() {
		DisplayMetrics dm = new DisplayMetrics();
		((WindowManager) HappyReaderApplication.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
		density = dm.density;
		screenHeight = dm.heightPixels;
		screenWidth = dm.widthPixels;
	}
	
	public static float getDensity() {
		if (density == 0) {
			getScreenInfo();
		}
		return density;
	}
	
	public static int getScreenWidth() {
		if (screenWidth == 0) {
			getScreenInfo();
		}
		return screenWidth;
	}
	
	public static int getScreenHeight() {
		if (screenHeight == 0) {
			getScreenInfo();
		}
		return screenHeight;
	}
	
	public static String getCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}
	
	public static String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}
}
